/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import POJOS.MusicGroup;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import DB.DBmanager;
import POJOS.Artist;

/**
 *
 * @author jim
 */
public class ListMusicGroupForm extends javax.swing.JFrame {
    int selectedRow;
    MusicGroup musicGroup;

    /**
     * Creates new form ListMusicGroupForm
     */
    public ListMusicGroupForm() {
        initComponents();
        checkButtons();
        //Επιλογή μόνο μίας γραμμής στον πίνακα άλμπουμ
        musicGroupTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }
    
    private void checkButtons(){
        selectedRow= musicGroupTable.getSelectedRow();
        modifyMusicGroup.setEnabled(selectedRow>=0);
        
        if (selectedRow>=0){
            musicGroup= musicGroupList.get(musicGroupTable.convertRowIndexToModel(selectedRow));
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadioStationPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadioStationPU").createEntityManager();
        musicGroupQuery = java.beans.Beans.isDesignTime() ? null : RadioStationPUEntityManager.createQuery("SELECT m FROM MusicGroup m");
        musicGroupList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(musicGroupQuery.getResultList());
        jLabel1 = new javax.swing.JLabel();
        exit = new javax.swing.JButton();
        modifyMusicGroup = new javax.swing.JButton();
        addDelete = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        musicGroupTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ΑΡΧΕΙΟ ΣΥΓΚΡΟΤΗΜΑΤΩΝ");
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(204, 0, 204));
        jLabel1.setText("Πίνακας Συγκροτημάτων");

        exit.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Button-exit-icon.jpg"))); // NOI18N
        exit.setText("ΕΞΟΔΟΣ");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });

        modifyMusicGroup.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        modifyMusicGroup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/hp-customization-control-panel-icon.png"))); // NOI18N
        modifyMusicGroup.setText("ΤΡΟΠΟΙΗΣΗ");
        modifyMusicGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyMusicGroupActionPerformed(evt);
            }
        });

        addDelete.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        addDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add-remove-user-icons-20638746.jpg"))); // NOI18N
        addDelete.setText("ΠΡΟΣΘΗΚΗ-ΔΙΑΓΡΑΦΗ");
        addDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addDeleteActionPerformed(evt);
            }
        });

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, musicGroupList, musicGroupTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nameMusicGroup}"));
        columnBinding.setColumnName("ΕΠΩΝΥΜΙΑ ΣΥΓΚΡΟΤΗΜΑΤΟΣ");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${formationDate}"));
        columnBinding.setColumnName("ΗΜΕΡΟΜΗΝΙΑ ΔΗΜΙΟΥΡΓΙΑΣ");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        musicGroupTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                musicGroupTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(musicGroupTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(addDelete)
                        .addGap(33, 33, 33)
                        .addComponent(modifyMusicGroup)
                        .addGap(35, 35, 35)
                        .addComponent(exit)))
                .addContainerGap(29, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(141, 141, 141))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(modifyMusicGroup)
                    .addComponent(exit))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        // TODO add your handling code here:
        //Επιβεβαίωση για την έξοδο
        int choice = JOptionPane.showConfirmDialog(null,"Επιθυμείτε έξοδο από τη διαχείριση συγκροτημάτων;","",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE); 
        
        if (choice==0){
            dispose();
        }else{
            checkButtons();
        }
    }//GEN-LAST:event_exitActionPerformed

    private void modifyMusicGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyMusicGroupActionPerformed
        // TODO add your handling code here:
        //Θέτει στο musicGroup την επιλεγμένη γραμμή του πίνακα και καλεί το GUI για την τροποποίηση γκρουπ
        selectedRow= musicGroupTable.getSelectedRow();
        musicGroup= musicGroupList.get(musicGroupTable.convertRowIndexToModel(selectedRow));
        new NewOrModifyMusicGroupForm(musicGroup).setVisible(true);
    }//GEN-LAST:event_modifyMusicGroupActionPerformed

    private void addDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addDeleteActionPerformed
        // TODO add your handling code here:
        //Αν δεν υπάρχει επιλεγμένη γραμμή στον πίνακα καλεί το GUI για την εισαγωγή νέου συγκροτήματος
        if (selectedRow<0 ){
            new NewOrModifyMusicGroupForm().setVisible(true);
        }else{
            //Για την διαγραφή
            selectedRow= musicGroupTable.getSelectedRow();
            musicGroup= musicGroupList.get(musicGroupTable.convertRowIndexToModel(selectedRow));
        
            //Επιβεβαίωση για τη διαγραφή
            int choice = JOptionPane.showConfirmDialog(null,"Επιθυμείτε να διαγράψετε το συγκρότημα " + musicGroup.getNameMusicGroup()+ "" +
            " ;","",JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
        
            if (choice==0){
                //Έλεγχος αν το συγκρότημα έχει άλμπουμ
                if(!musicGroup.getAlbumCollection().isEmpty()){
                    //Αν έχει το συγκρότημα άλμπουμ αδύνατη διαγραφή
                    JOptionPane.showMessageDialog(null,"Αδύνατη διαγραφή συγκροτήματος!Το συγκρότημα περιέχει άλμπουμ!","ERROR",JOptionPane.ERROR_MESSAGE);
                }else{
                    
                    //Αφαίρεση καλλιτεχνών από το συγκρότημα
                    if(DB.DBmanager.deleteArtistAndMusicGroup(musicGroup, musicGroup.getArtistCollection())){
                        //Διαγραφή συγκροτήματος
                        if (DB.DBmanager.DeleteMusicGroup(musicGroup)){
                            musicGroupList.remove(musicGroup);
                   
                            JOptionPane.showMessageDialog(null,"Επιτυχής διαγραφή συγκροτήματος!","SUCCESSFULL",JOptionPane.INFORMATION_MESSAGE);
                        }
                    }else{
                        JOptionPane.showMessageDialog(null,"Αποτυχία διαγραφής συγκροτήματος!","ERROR",JOptionPane.ERROR_MESSAGE);
                    }  
                }
            }                                               
        } 
    }//GEN-LAST:event_addDeleteActionPerformed

    private void musicGroupTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_musicGroupTableMouseClicked
        // TODO add your handling code here:
        checkButtons();
    }//GEN-LAST:event_musicGroupTableMouseClicked

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
        //Με κλικ στο GUI εκτός πίνακα ενεργοποιείται το GUI για την εισαγωγή γκρουπ
        selectedRow=-1;
        modifyMusicGroup.setEnabled(selectedRow>=0);
    }//GEN-LAST:event_formMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ListMusicGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ListMusicGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ListMusicGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ListMusicGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ListMusicGroupForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager RadioStationPUEntityManager;
    private javax.swing.JButton addDelete;
    private javax.swing.JButton exit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton modifyMusicGroup;
    public static java.util.List<POJOS.MusicGroup> musicGroupList;
    public static javax.persistence.Query musicGroupQuery;
    public static javax.swing.JTable musicGroupTable;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
